package com.aos.erp.schools.service.coach.impl;


import com.aos.erp.common.bean.IPageBean;
import com.aos.erp.common.bean.PageBean;
import com.aos.erp.common.dao.mapper.Condition;
import com.aos.erp.common.exception.BizException;
import com.aos.erp.schools.mapper.coach.CoachMapper;
import com.aos.erp.schools.mapper.coach.CoachPictureMapper;
import com.aos.erp.schools.mapper.gatherschool.GatherSchoolMapper;
import com.aos.erp.schools.model.coach.*;
import com.aos.erp.schools.model.division.vo.ShowDivisionVo;
import com.aos.erp.schools.model.gatherschool.vo.GatherSchoolVo;
import com.aos.erp.schools.service.coach.CoachPictureService;
import com.aos.erp.schools.service.coach.CoachService;
import com.aos.erp.schools.util.UserUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Service
public class CoachServiceImpl extends ServiceImpl<CoachMapper,Coach> implements CoachService {

    @Resource
    private CoachPictureService coachPictureService;
    @Resource
    private CoachMapper coachMapper;
    @Resource
    private GatherSchoolMapper gatherSchoolMapper;


    @Override
    public Coach getById(long id) {
        return baseMapper.selectById(id);
    }

    @Override
    public void addBean(AddCoachRo addCoachRo) throws Exception {
        //根据用户ID获取当前登录用户所属驾校
        String schoolId=UserUtil.getUserSchoolId(addCoachRo.getCurrentUserId());
        Coach coach=new Coach();
        BeanUtils.copyProperties(addCoachRo,coach);
        Date date=new Date();
        coach.setDriversSchoolId(Long.parseLong(schoolId));
        coach.setCreated(date);
        coach.setUpdated(date);
        coach.setIsDel(0);
        coach.setIsAuthentication(0);//默认未认证
        System.out.println(coach.toString());
        baseMapper.insert(coach);
        //增加驾驶证图片
        CoachPicture coachPicture=new CoachPicture();
        coachPicture.setCoachId(coach.getId());
        coachPicture.setCreated(date);
        coachPicture.setFileId(addCoachRo.getCoachLicense());
        coachPicture.setUpdated(date);
        coachPicture.setType(1);//驾驶证
        coachPicture.setIsDel(0);
        coachPictureService.addBean(coachPicture);
        //增加教练图片
        CoachPicture card=new CoachPicture();
        card.setCoachId(coach.getId());
        card.setCreated(date);
        card.setUpdated(date);
        card.setFileId(addCoachRo.getCoachCard());
        card.setType(2);//教练证
        card.setIsDel(0);
        coachPictureService.addBean(card);
        //增加教练图片
        CoachPicture head=new CoachPicture();
        head.setCoachId(coach.getId());
        head.setCreated(date);
        head.setUpdated(date);
        head.setFileId(addCoachRo.getCoachHead());
        head.setType(3);//教练头像
        head.setIsDel(0);
        coachPictureService.addBean(head);

    }

    @Override
    public IPageBean queryByPage(QueryCoachRo queryCoachRo) throws Exception {
        Map<String,Object> map=new HashMap<String, Object>();
        IPage<ShowDivisionVo> page = new Page<ShowDivisionVo>(queryCoachRo.getPageIndex()==null?1:queryCoachRo.getPageIndex(),queryCoachRo.getPageSize()==null?10:queryCoachRo.getPageSize());
        map.put("coach",queryCoachRo.getCoach());
        map.put("depCode",UserUtil.getUserDepCode(queryCoachRo.getCurrentUserId()));
        map.put("stime",queryCoachRo.getStime());
        map.put("etime",queryCoachRo.getEtime());
        map.put("trainCrtype",queryCoachRo.getTrainCrtype());
        map.put("timeType",queryCoachRo.getTimeType());
        map.put("IPage",page);
        List<ShowDivisionVo> list=coachMapper.queryList(map);
        page.setRecords(list);
        return baseMapper.toBizPage(page);
    }

    @Override
    public void delCoach(long id) throws BizException {
        Coach coach=this.baseMapper.selectById(id);
        if(coach==null){
            throw new BizException("教练数据错误");
        }
        coach.setIsDel(-1);//逻辑删除
        this.baseMapper.updateById(coach);
    }

    @Override
    public CoachInfoVo coachInfo(long id) {
        Coach coach=this.baseMapper.selectById(id);
        CoachInfoVo coachInfoVo=new CoachInfoVo();
        BeanUtils.copyProperties(coach,coachInfoVo);
        Map<String,Object> map=new HashMap<String,Object>();
        map.put("coachId",coach.getId());
        List<CoachPicture> list=coachPictureService.queryList(map);
        for (CoachPicture coachPicture:list) {
            Integer type=coachPicture.getType();
            if(type==1){
                //教练驾驶证
                coachInfoVo.setCoachCard(coachPicture.getFileId()+"");
            }else if(type==2){
                //教练证
                coachInfoVo.setCoachLicense(coachPicture.getFileId()+"");
            }else if(type==3){
                //头像
                coachInfoVo.setCoachHead(coachPicture.getFileId()+"");
            }
        }
        return coachInfoVo;
    }

    @Override
    public int updateCoach(UpdateCoachRo updateCoachRo) {
        System.out.println(updateCoachRo.toString());
        Date date=new Date();
        UpdateCoach updateCoach=new UpdateCoach();
        BeanUtils.copyProperties(updateCoachRo,updateCoach);
        System.out.println(updateCoach.toString());
        updateCoach.setUpdated(date);
        updateCoach.setSex(updateCoachRo.getSexDesc());
        coachMapper.updateCoach(updateCoach);
        //修改教练图片
        Map<String,Object> map=new HashMap<String,Object>();
        map.put("coachId",updateCoachRo.getId());
        List<CoachPicture> list=coachPictureService.queryList(map);
        for (CoachPicture coachPicture:list) {
            int type=coachPicture.getType();
            if(type==1){
                //教练证
                coachPicture.setFileId(updateCoachRo.getCoachLicense());
            }else if(type==2){
                //教练驾驶证
                coachPicture.setFileId(updateCoachRo.getCoachCard());
            }else if(type==3){
                //教练头像
                coachPicture.setFileId(updateCoachRo.getCoachHead());
            }
            coachPicture.setUpdated(date);
            coachPictureService.updatePic(coachPicture);
        }
        return 1;
    }

    @Override
    public IPage<Coach> queryCoachBySchoolId(Integer pageIndex,Integer pageSize,String schoolId,String name) throws BizException {
        if("".equals(schoolId)||schoolId==null){
            throw new BizException("驾校Id不能为空");
        }
        GatherSchoolVo gatherSchoolVo=gatherSchoolMapper.queryObject(Long.parseLong(schoolId));
        if(gatherSchoolVo==null){
            throw new BizException("驾校不存在");
        }
        IPage<Coach> page = new Page<Coach>(pageIndex==null?1:pageIndex,pageSize==null?10:pageSize);
        Map<String,Object> map=new HashMap<>();
        map.put("schoolId",schoolId);
        map.put("name",name);
        map.put("page",page);
        List<Coach> list=coachMapper.queryListInfo(map);
        page.setRecords(list);
        return page;
    }
}